/**
 * 我的发文js
 */
$(document).ready(function () {

    //get sent document count and initial tables
    $.ajax({
        type: 'GET',
        url: '../document/count/outbox',
        contentType: "application/json",
        dataType: 'JSON',
        success: function (json) {
            var outboxCount = json.data.sent;
            var pagesNumber = parseInt(outboxCount / 10 + 1);
            var $checkbox_paginate = $("#datatable-checkbox_paginate");
            var $ulObj = $(".pagination",$checkbox_paginate);

            createPaging($checkbox_paginate,pagesNumber);   //初始化分页元素
            displayOutboxList(1);    //初始化表格

            var $liObj = $("li",$ulObj);

            for(var i = 1;i < $liObj.length - 1;i++){
                $liObj.eq(i).children("a").attr("onclick",'displayOutboxList('+ i +')');
            }   //end for
        },
    });

});

function docContent(e,doc_id){
    var url = "../document/my_doc_content.html?doc_id=" + doc_id;
    window.location.href = url;
}
//初始化表格
function initTable(data){
    var $datatable = $('#datatable-checkbox');
    $datatable.dataTable({
        data:data,
        "createdRow": function ( row, data, index ) {
            $('td', row).css("text-align","center").css("vertical-align","middle");
        },
        columns:[
            {"sTitle": "发送时间"},
            {"sTitle": "公文标题"},
            {"sTitle": "公文号"},
            {'sTitle': "公文文种"},
            {"sTitle": "紧急程度"},     //4
            {"sTitle": "接收人员"},
            {'sTitle': "备注"},
            {"sTitle": "公文状态"},
        ],
        "ordering": false,
        deferRender: true,
        paging:false
    } );

}

// 公文撤销
function docRevoked(doc_id) {
    if(confirm("是否撤销该公文?")){
        $.ajax({
            type: 'PUT',
            url: '../document/sent/revoked/' + doc_id,
            contentType: "application/json",
            dataType: 'JSON',
            success: function (json,status) {
                if(json.err_msg == null){
                    new PNotify({
                        title: '提示',
                        text: '该公文已被撤销',
                        type: 'success',
                        hide: false,
                        styling: 'bootstrap3'
                    });
                    window.location.href = "../document/my_sent_doc.html";
                }else{
                    alert(json.err_msg);
                }

            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.log(errorThrown);
            },
        })
    }
}

// 公文发送
function docSent(doc_id) {
    if(confirm("是否发送该条公文?")){
        $.ajax({
            type: 'POST',
            url: '../document/sent/direct/' + doc_id,
            contentType: "application/json",
            dataType: 'JSON',
            success: function (json,status) {
                if(json.err_msg == null){
                    window.location.href = "../document/my_sent_doc.html";
                }else{
                    alert(json.err_msg);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.log(errorThrown);
            },
        })
    }
}

/* 显示发文列表
 * @param:pagesNumber
 * */
function displayOutboxList(pagesNumber) {
    clearTables($('#datatable-checkbox'));
    adjustPagination($('#pagination'),pagesNumber);

    // 获取收文列表
    $.ajax({
        type: 'POST',
        url: '../document/list/outbox',
        contentType: "application/json",
        data:JSON.stringify({
            "pageNumber": parseInt(pagesNumber),
        }),
        dataType: 'JSON',
        success: function (json,status) {
            var datas = new Array();
            var arr = new Array();
            var docs = json.data.documents;
            if(docs != null){
                for(var i = 0;i<docs.length;i++){
                    arr = [];
                    arr[0] = docs[i].sent_time;
                    arr[1] = docs[i].title;
                    arr[2] = docs[i].number;
                    arr[3] = docs[i].type;
                    arr[4] = docs[i].serious_type;
                    if(docs[i].serious_type == "NOT_SERIOUS"){
                        arr[4] = "不紧急";
                    }else if(docs[i].serious_type == "SERIOUS"){
                        arr[4] = "紧急";
                    }
                    /*var $received = docs[i].received;
                    arr[5] = $received.toString();*/
                    var received = docs[i].received;
                    var html = "<a class='btn btn-success btn-xs' onclick='viewDept(this)'>查看所有</a>";
                    html += "<ul style='display:none' class='rcv-ul'>";
                    for(var j = 0;j < received.length;j++){
                        html += "<li>" + received[j] + "</li>";
                    }
                    html += "</ul>";
                    arr[5] = html;
                    arr[6] = docs[i].remarks;
                    arr[7] = "<a href='#' class='btn btn-info' onclick='docContent(this,"+ docs[i].id + ")'>查看公文 </a>";
                    var $approval = docs[i].approval;
                    if($approval == 1){
                        arr[7] += "<button type='button' class='btn btn-warning' onclick='docRevoked(" + docs[i].id +")'>公文撤销</button><br/>";
                        arr[7] += "阅读数量: " + docs[i].reader_count + "/" + docs[i].receiver_count;
                    }else if($approval == 0){
                        arr[7] += "<button type='button' class='btn btn-primary' onclick='docSent("+ docs[i].id +")'>发送公文</button>";
                        arr[7] += "<br/>公文未发送";
                    }
                    // 该公文已被撤销
                    if(docs[i].revoked == true){
                        arr[7] = "<a href='#' class='btn btn-info' onclick='docContent(this,"+ docs[i].id + ")'>查看公文 </a>";
                        arr[7] += "<br/><p style='color: orangered'>该公文已被撤销</p> "
                    }
                    datas[i] = arr;
                }
                initTable(datas);

            }
            if(json.err_msg != null){
                alert(json.err_msg);
            }
        }
    });
}

function viewDept(e){
    var css = $(e).next().css('display');
    if(css == 'none'){
        $(e).text('隐藏部门');
        $(e).next().css('display', 'block');
    }
    else{
        $(e).text('查看所有');
        $(e).next().css('display', 'none');
    }
}